﻿
angular.module('WebApiServices', ['ngResource'], function ($provide) {

    $provide.factory('CartService', function ($resource, $http) {
        var svc = {
            getCurrent: function (cartCode) {
                if (cartCode == null) {
                    var result = $http({
                        method: 'GET',
                        url: '/api/cartapi/GetCurrent',
                        cache: false
                    });
                    return result;
                }
                else {
                    var result = $http({
                        method: 'GET',
                        url: '/api/cartapi/GetCartByCode/' + cartCode,
                        cache: false
                    });
                    return result;
                }
            },
            addToCart: function (product, quantity) {
                var result = $http({
                    method: 'PUT',
                    url: '/api/cartapi/putcartitem',
                    params: {
                        productCode: product.Code,
                        quantity: quantity,
                        offerName: null,
                        addFrom: null
                    }
                });
                return result;
            },
            deleteItem: function (index) {
                var result = $http({
                    method: 'DELETE',
                    url: '/api/cartapi/deletecartitem/' + index,
                });
                return result;
            },
            changeQuantity: function (index, cartItem) {
                var result = $http({
                    method: 'POST',
                    url: '/api/cartapi/PostCartItemQuantity',
                    params: {
                        index: index,
                        quantity: cartItem.Quantity
                    }
                });
                return result;
            }
        };

        return svc;
    });

    $provide.factory('CatalogService', function ($resource, $http) {
        var svc = {
            getCategoryList: function () {
                var result = $http.get('/api/catalogapi/getproductcategorylist');
                return result;
            },
            getCategoryByLink: function (link) {
            	var result = $http.get('/api/catalogapi/getproductcategorybylink/' + link);
                return result;
            },
            getProductList: function (filter) {
                var result = $http({
                    method: 'POST',
                    url: '/api/catalogapi/getproductlistbyfilter',
                    data: filter
                });
                return result;
            },
            getProductByCode: function (code) {
                var result = $http({
                    method: 'GET',
                    url: '/api/catalogapi/getproductbycode/' + code
                });
                return result;
            }
        };
        return svc;
    });

    $provide.factory('OfferService', function ($resource, $http) {
        var svc = {
            getProductList: function (filter) {
                var result = $http({
                    method: 'GET',
                    url: '/api/offersapi/GetProductList',
                    params: filter
                });
                return result;
            }
        };

        return svc;
    });

    $provide.factory('AccountService', function ($resource, $http) {
        var svc = {
            getUserInfo: function () {
                var result = $http.get('/api/accountapi/getuserinfo')
                return result;
            }
        };
        return svc;
    });

    $provide.factory('IncentiveService', function ($resource, $http) {
        var svc = {
            applyCoupon: function (couponCode, cartCode) {
                var result = $http({
                    method: 'PUT',
                    url: '/api/incentiveapi/PutCouponToCart/' + couponCode + '?cartCode=' + cartCode,
                });
                return result;
            },
            removeCoupon: function (couponCode) {
                var result = $http({
                    method: 'DELETE',
                    url: '/api/incentiveapi/deletecoupon/' + couponCode
                });
                return result;
            }
        };
        return svc;
    });
});